package game

import (
	"github.com/hwcer/cosgo/binder"
	"github.com/hwcer/yyds/context"
	"github.com/hwcer/yyds/options"
	"server/game/define/helper"
)

func init() {
	context.Serialize = serialize
}

//type msg struct {
//	*values.Message
//	Time   int64                `json:"time,omitempty"`
//	Cache  []any                `json:"cache,omitempty"`
//	Dirty  []*operator.Operator `json:"dirty,omitempty"`
//	Notify values.Byte          `json:"notify,omitempty"` //消息通知
//}
//
//type cache struct {
//	*operator.Operator
//}
//
//func (this *cache) convert() any {
//	r := map[string]any{}
//	r["t"] = this.Type.ToString()
//	if this.Key != "" {
//		r["k"] = this.Key
//	} else if this.Type == operator.TypesAdd || this.Type == operator.TypesSub {
//		r["k"] = "val"
//	}
//	r["v"] = this.Value
//	r["b"] = this.Bag
//	r["id"] = this.IID
//	r["_id"] = this.OID
//	r["r"] = this.Result
//	return r
//}

func serialize(c *context.Context, reply *context.Message) ([]byte, error) {
	b := c.Binder(binder.ContentTypeModRes)

	if options.HasServiceMethod(c.ServiceMethod()) {
		//v := &msg{Message: reply.Message, Time: reply.Time, Notify: reply.Notify}
		//for _, s := range reply.Cache {
		//	s2 := cache{Operator: s}
		//	v.Cache = append(v.Cache, s2.convert())
		//}
		//消息推送
		if reply.Code == 0 && c.Player != nil {
			notify := helper.NewNotify(c.Player.Updater)
			reply.Notify = notify.Get()
		}
	}
	return b.Marshal(reply)

}
